document.addEventListener('plusready',function(){
	$('.userpic').on('click',function(){
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
	
	//添加图片
	var index = 1;
	var files = [];
	function appendpic(p){
		plus.nativeUI.showWaiting('图片处理中...');
		//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
		var newpath = p.replace(/\./g , new Date().getTime()+'.');
		
		plus.zip.compressImage({
				src : p,
				dst : newpath,
				quality : 20
			},
			function(){//毁掉成功
				files.push({name : "uploadkey" + index,path:p});
				index++;
				upload();
				
			},
			function(error){
			alert('压缩图片失败');
		})
	}
	
	//处理
	function upload(){
		
		if(files.length <= 0){
			toast("请上传头像！");
		}
		var server = apiRoot + "?act=member_data&Op=uploadoneopic";  
		var task = plus.uploader.createUpload(server,
		    {method:"post"},
			function(t,status){ //上传完成
				if(status == 200){
					console.log(JSON.stringify(t));
//					console.log(webRoot+t.responseText);
					var upic = webRoot+t.responseText;
					console.log(upic);
					$(".shangchuan").attr('src',upic);
//					plus.storage.setItem('pic',t.responseText)
					plus.nativeUI.closeWaiting();
//					plus.nativeUI.toast('处理完成' + t.responseText);
				}else{
					console.log(JSON.stringify(t));
					toast('上传失败：' + status);
					plus.nativeUI.closeWaiting();
				}
				
			},function () {
				console.log(111);
				plus.nativeUI.closeWaiting();
			}
			
		);
		for(var i = 0;i　<　files.length ; i++){
			var f =files[i];
			task.addFile(f.path,{key:f.name});
		}
		task.start();
	}
},false)